<template>
  <div class="home">
    <el-row display="margin-top:10px">
        <el-input v-model="input" placeholder="请输入书名" style="display:inline-table; width: 30%; float:left"></el-input>
        <el-button type="primary" @click="addBook()" style="float:left; margin: 2px;">新增</el-button>
    </el-row>
    <el-row>
        <el-table :data="bookList" style="width: 100%" border>
            <el-table-column  type="index"  width="50" label="序号">
            </el-table-column>

          <el-table-column prop="book_name" label="书名" min-width="100">
            <template slot-scope="scope"> {{ scope.row.fields.book_name }} </template>
          </el-table-column>
          <el-table-column prop="add_time" label="添加时间" min-width="100">
            <template slot-scope="scope"> {{ scope.row.fields.add_time }} </template>
          </el-table-column>
            <el-table-column
             
             label="操作"
             width="120">
             <template slot-scope="scope">
               <el-button
                @click.native.prevent="deleteRow(scope.row.pk, bookList)"
                type="text"
                size="small">
                删除
            </el-button>
      </template>
    </el-table-column>

        </el-table>
    </el-row>
    <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
  </div>
  
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      input: '',
      currentPage: 1,
      pagesize: 5,
      total: 1,
      bookList: []
    }
  },
  mounted: function() {
      this.showBooks(this.currentPage,this.pagesize)
  },
  methods: {
    deleteRow(index, rows) {
        this.$http.post('http://127.0.0.1:8000/api/del_books',{bookId:index},{emulateJSON:true})
        .then((response) => {
            var res = JSON.parse(response.bodyText)
            console.log(res.total) 
            if (res.error_num == 0) {
              this.showBooks(this.currentPage, this.pagesize)
            } else {
              this.$message.error('查询删除失败')
              console.log(res['msg'])
            }
        })
    },
    handleSizeChange(val) {
        this.showBooks(this.currentPage, val)
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.showBooks(val,this.pagesize)
        console.log(`当前页: ${val}`);
      },
    addBook(){
      this.$http.get('http://127.0.0.1:8000/api/add_book?book_name=' + this.input)
        .then((response) => {
            var res = JSON.parse(response.bodyText)
            if (res.error_num == 0) {
              this.showBooks(this.currentPage, this.pagesize)
            } else {
              this.$message.error('新增书籍失败，请重试')
              console.log(res['msg'])
            }
        })
    },
    showBooks :function(pageNum,pagesize){
      this.$http.post('http://127.0.0.1:8000/api/show_books',{pageNum:pageNum, pageSize:pagesize},{emulateJSON:true})
        .then((response) => {
            var res = JSON.parse(response.bodyText)
            console.log(res.total) 
            if (res.error_num == 0) {
              this.total=res.total
              this.bookList = res['list']
            } else {
              this.$message.error('查询书籍失败')
              console.log(res['msg'])
            }
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
